package com.ddl.common.util;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @author dudianbo
 * @ClassName: ${CLASSNAME}
 * @Description: ${DESCRIPTION}
 * @date 2018/7/3
 */
public class ConfigProperties {
    private static final Logger logger = LoggerFactory.getLogger(ConfigProperties.class);
    private static final Map<String, String> lookupMap = new HashMap<String, String>();

    static {
        Properties config = loadConfigInfo("properties/config.properties");
        Enumeration en = config.keys();
        while (en.hasMoreElements()) {
            String key = en.nextElement().toString();
            String value = config.getProperty(key);
            lookupMap.put(key, value);
        }

    }

    public static String getValue(String key) {
        if (StringUtils.isBlank(key)){
            return "";
        }
        String value = lookupMap.get(key);
        return value == null ? "" : value;
    }

    public static Properties loadConfigInfo(String configFileName) {
        Properties config = new Properties();
        InputStream in = null;
        try {
            in = ConfigProperties.class.getClassLoader().getResourceAsStream(configFileName);
            config.load(in);
        } catch (Exception e) {
            logger.error("加载配置文件失败",e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    logger.error("加载配置文件失败",e);
                }
            }
        }
        return config;
    }

    public static void main(String[] args) {
        System.out.println(ConfigProperties.getValue("eth_ip"));
    }
}
